package com.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.Book;
import com.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author：李刚
 * @Date： 2020/12/7-14:59
 * @Description：
 */
@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    public List<Book> findAll(){
        while(redisTemplate.opsForList().size("books")>0){
            redisTemplate.opsForList().leftPop("books");
        }
        redisTemplate.opsForList().rightPushAll("books",bookMapper.selectList(null));
        return bookMapper.selectList(null);
    }

    //获取book策略：先从缓存中获取book,没有则在数据库中获取，再将数据写入缓存
    public Book findBookById(int id){
        String key = "book_"+id;
        ValueOperations<String,Book> operations = redisTemplate.opsForValue();
        //判断redis中是否有键为key的缓存
        boolean hasKey = redisTemplate.hasKey(key);
        if(hasKey){
            Book book = operations.get(key);
            System.out.println("从缓存中获得数据："+book.getBookName());
            System.out.println("-----------------------------------");
            return book;
        }else{
            Book book = bookMapper.selectById(id);
            System.out.println("从数据库中获得数据："+book.getBookName());
            System.out.println("-----------------------------------");
            //写入缓存
            operations.set(key,book,5, TimeUnit.HOURS);
            return book;
        }
    }
    //增加图书信息
    public int insertBook(Book book){
        int flag = bookMapper.insert(book);
        return flag;
    }
    //更新图书策略：先更新数据表，成功之后，删除原来的缓存，再更新缓存
    public int updateBook(Book book){
        ValueOperations<String,Object> operations = redisTemplate.opsForValue();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("book_id",book.getBookId());
        int result = bookMapper.update(book,queryWrapper); //更新数据表
        if(result !=0 ){
            String key = "book_"+book.getBookId();
            boolean haskey = redisTemplate.hasKey(key);
            if(haskey){
                redisTemplate.delete(key);
                System.out.println("删除缓存中的key------------> "+key);
            }
            //再将更新后的数据加入缓存
            Book newbook = bookMapper.selectById(book.getBookId());
            if(newbook != null){
                operations.set(key,newbook,3,TimeUnit.HOURS);
            }
        }
        return result;
    }
    //删除图书策略：删除数据表中的数据，然后删除缓存
    public int deleteBookById(int id){
        int result = bookMapper.deleteById(id); //删除数据库中记录
        String key = "book_"+id;
        if(result != 0){
            boolean hasKey = redisTemplate.hasKey(key); //查询缓存是否存在对应的id
            if(hasKey){
                redisTemplate.delete(key);//删除缓存中的数据
                System.out.println("删除了缓存中的key："+key);
            }
        }
        return result;
    }
}

